
# -*- coding: utf-8 -*-import datetime
from django.db import models
from django.utils import timezone
import json

# Create your models here.
class Poudre(models.Model):
    nom = models.CharField(max_length=200)
    codeP = models.CharField(max_length=200)
    dateP = models.DateTimeField('last operation')
    instock = models.FloatField(default=0)
    
    def __unicode__(self):
        return self.nom
    
class Type_Event (models.Model):
    nomTE = models.CharField(max_length=200)
    def __unicode__(self):
        return self.nomTE

class Event(models.Model):
    poudre = models.ForeignKey(Poudre)
    TE = models.ForeignKey(Type_Event)
    date = models.DateTimeField()
    quantite = models.IntegerField(default=0)
    prix = models.FloatField(default=0)
    somme = models.FloatField(default=0)
    comment = models.TextField()
    instock = models.FloatField(default=0)
        
    def __unicode__(self):
        return self.TE.nomTE
    
    def update(self):
        p = self.poudre
        if (self.TE.nomTE  == "Xuất"):
            p.instock -= self.quantite
        elif (self.TE.nomTE  == "Nhập"):
            p.instock += self.quantite
        p.save()
        self.instock = p.instock
        self.save()
        return True
        

